package com.xxl.job.executor.crawler.util;

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

/**
 * 描述: 封装redisUtil类
 *
 * @author Pfly
 * @create 2019-01-05 9:32
 */
public class JedisUtil {
    private static JedisPool pool = null;

    static {
        // 创建一个redis的连接池
        Properties prop = new Properties();
        try {
            prop.load(JedisUtil.class.getClassLoader().getResourceAsStream("redis.properties"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        int maxIdle = new Integer(prop.getProperty("redis.maxIdle"));
        int minIdle = new Integer(prop.getProperty("redis.minIdle"));
        int maxTotal = new Integer(prop.getProperty("redis.maxTotal"));
        poolConfig.setMaxIdle(maxIdle);// 最大闲置个数
        poolConfig.setMinIdle(minIdle);// 最小闲置个数
        poolConfig.setMaxTotal(maxTotal);// 最大连接数
        String host = prop.getProperty("redis.host");
        int port = new Integer(prop.getProperty("redis.port"));
        pool = new JedisPool(poolConfig, host, port);
    }

    /**
     *  调用连接redis
     * @return
     */
    public static Jedis getJedis() {
        return pool.getResource();
    }


    /**
     *  查询方法
     *  查询所有的失败的url
     */
    @Test
    public static List selectSepcialUrlByRedis(){
        Jedis jedis = JedisUtil.getJedis();
        List<String> urlAll = jedis.lrange("sepcialUrl", 0, -1);
        return urlAll;
    }

    public static List selectDetailUrlByRedis(){
        Jedis jedis = JedisUtil.getJedis();
        List<String> urlAll = jedis.lrange("detailUrl", 0, -1);
        return urlAll;
    }

    public static List selectproductUrlByRedis(){
        Jedis jedis = JedisUtil.getJedis();
        List<String> urlAll = jedis.lrange("productUrl", 0, -1);
        return urlAll;
    }

    //变数组
    /*public String[] List selectUrlByRedis(){
        Jedis jedis = JedisUtil.getJedis();
        List<String> urlAll = jedis.lrange("url", 0, -1);
        String[] urls = new String[urlAll.size()];
        for (int i = 0; i < urlAll.size(); i++) {
            String url = jedis.lpop("url");
        }
        return urlAll;
    }*/

    /**
     *  插入方法
     *  插入失败的url
     */
    public static Long saveSepcialUrlByRedis(String url){
        if (url != null) {
            Jedis jedis = JedisUtil.getJedis();
            Long result = jedis.rpush("sepcialUrl", url);
            return result;
        }
        return 0L;
    }

    public static Long saveDetailUrlByRedis(String url){
        if (url != null) {
            Jedis jedis = JedisUtil.getJedis();
            Long result = jedis.rpush("detailUrl", url);
            return result;
        }
        return 0L;
    }


    public static Long saveProductUrlByRedis(String url){
        if (url != null) {
            Jedis jedis = JedisUtil.getJedis();
            Long result = jedis.rpush("productUrl", url);
            return result;
        }
        return 0L;
    }
}
